package command

import (
	"fmt"
	"time"

	"gitee.com/krio/ginc/gen"
	"gitee.com/krio/ginc/util"
	"github.com/spf13/cobra"
)

// genCmd represents the gen command
var genCmd = &cobra.Command{
	Use:   "gen",
	Short: "框架脚手架",
	Long:  `根据proto创建api相关：pb、doc、route（验证、中间件、权限等自定义功能）、handler`,
	Run: func(cmd *cobra.Command, args []string) {
		nowTs := time.Now().UnixNano() / 1e6
		err := gen.Gender.Run()
		if err != nil {
			util.PrintError(err)
			return
		}
		endTs := time.Now().UnixNano() / 1e6
		util.PrintSuccess("操作状态：成功")
		util.PrintSuccess(fmt.Sprintf("操作耗时：%s", util.ReadTime(endTs-nowTs)))
	},
}

func init() {
	rootCmd.AddCommand(genCmd)
	// 参数
	gen.Gender = &gen.Generator{}
	genCmd.Flags().StringVarP(&gen.Gender.FromProtoPath, "proto", "p", "", "设置PROTO文件目标路径")
	genCmd.Flags().StringVarP(&gen.Gender.FromPbPath, "pb", "b", "", "设置生成PB文件路径")
	genCmd.Flags().StringVarP(&gen.Gender.ToDocPath, "doc", "d", "", "设置生成文档文件路径")
	genCmd.Flags().StringArrayVarP(&gen.Gender.FromThirdPartyPath, "third_party", "t", nil, "设置Third-party引用路径")
	genCmd.Flags().StringVarP(&gen.Gender.ToHandlerPath, "handler", "c", "", "设置生成handler文件路径")
	genCmd.Flags().StringVarP(&gen.Gender.ToRoute, "router", "r", "", "设置路由文件")
}
